package com.wise.site.detail;

import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;
import com.wise.site.R;
import com.wise.site.utils.QQWeiboUtil;
import com.wise.site.utils.SinaWeiboUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class DetailShareActivity extends Activity implements View.OnClickListener {
	
	
	private static final int MESSAGE = 10000;
	private static final int SENDSINAWEIBO = MESSAGE + 1;
	private AlertDialog mDialog;
	private View mView;
	private String mTilte = "aaaaaa";
	private String mContent = "bbbbbb";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_share_activity);
		findViewById(R.id.share_to_phone_user).setOnClickListener(this);
		findViewById(R.id.share_to_email_contact).setOnClickListener(this);
		findViewById(R.id.share_to_sina_weibo).setOnClickListener(this);
		findViewById(R.id.share_to_qq_weibo).setOnClickListener(this);
		findViewById(R.id.cancel).setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		int id = v.getId();
		switch(id){
		case R.id.share_to_phone_user:
			intent.setAction(Intent.ACTION_VIEW);
			intent.setType("vnd.android-dir/mms-sms");
			intent.putExtra("sms_body", mContent); 
			startActivity(intent); 
			break;
		case R.id.share_to_email_contact:
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("plain/text");//模拟器/////模拟器也不能调用邮件程序
//			intent.setType("message/rfc822");//真机
			intent.putExtra(Intent.EXTRA_SUBJECT, mTilte);//主题
			intent.putExtra(Intent.EXTRA_TEXT, mContent);//内容
			startActivity(Intent.createChooser(intent, "请选择邮件发送程序"));
			break;
		case R.id.share_to_sina_weibo:
			if(SinaWeiboUtil.getAuthorizeUserName(this) == null){
				//先授权
				SinaWeiboUtil.callAuthorizeUI(this);
			}else{
				//直接分享
				SinaWeiboUtil.sendSinaWeibo(this, mContent);
			}
			break;
		case R.id.share_to_qq_weibo:
			if(QQWeiboUtil.getAuthorizeUserName(this) == null){
				//Toast.makeText(this, R.string.show_logo_qq_weibo, Toast.LENGTH_LONG).show();
				Message msg = new Message();
				msg.what = MESSAGE;
				msg.arg1 = R.string.show_logo_qq_weibo;
				mHandler.sendMessage(msg);
				QQWeiboUtil.callAuthorizeUI(this);
			}else{
				sendQQWeibo();
				/*new Thread("share to qq weibo"){
					public void run() {
						sendQQWeibo();
					};
				}.start();*/
			}
			break;
		case R.id.send_btn:
			if(mView != null){
				String content = ((EditText)mView.findViewById(R.id.content)).getText().toString();
				Message msg = new Message();
				if(TextUtils.isEmpty(content)){
					msg.what = MESSAGE;
					msg.arg1 = R.string.content_is_null;
					mHandler.sendMessage(msg);
				}else{
					int state = QQWeiboUtil.sendQQWeibo(this, content);
					if(state == 0){//发送成功
						msg.what = MESSAGE;
						msg.arg1 = R.string.share_success;
						mHandler.sendMessage(msg);
						if(mDialog != null && mDialog.isShowing()){
							mDialog.dismiss();
						}
						finish();
					}else if(state == 1){//发送失败
						
					}else if(state ==2){//获取验证码失败
						
					}else if(state == 3){//access失败
						
					}
				}
			}
			break;
		case R.id.close_btn:
			if(mDialog != null && mDialog.isShowing()){
				mDialog.dismiss();
			}
			break;
		case R.id.cancel:
			finish();
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == QQWeiboUtil.RESULT && resultCode == OAuthV2AuthorizeWebView.RESULT_CODE){
			OAuthV2 oauth = (OAuthV2) data.getExtras().getSerializable("oauth");
			String msg = oauth.getMsg();
			String split[] = oauth.getMsg().split("&");
			String userName = split[split.length - 2].split("=")[1];
			
			if(QQWeiboUtil.authorize(this, userName, oauth.getAccessToken(), oauth.getOpenid(), oauth.getClientId())){
				sendQQWeibo();
				//QQWeiboUtil.sendQQWeibo(this, "分享到腾讯微博测试");
				/*new Thread("share to qq weibo"){
					public void run() {
						sendQQWeibo();
					};
				}.start();*/
			}
		}
	}
	
	private void sendQQWeibo(){
		
		if(mDialog == null){
			mDialog = new AlertDialog.Builder(this).create();
		}
		if(mView == null){
			mView = LayoutInflater.from(this).inflate(R.layout.qq_weibo_content, null);
			mView.findViewById(R.id.close_btn).setOnClickListener(this);
			mView.findViewById(R.id.send_btn).setOnClickListener(this);
			
		}
		
		((TextView)mView.findViewById(R.id.title)).setText(mTilte);
		((TextView)mView.findViewById(R.id.content)).setText(mContent);
		
		mDialog.setView(mView, 0, 0, 0, 0);
		
		mDialog.show();
		
		
		//Toast.makeText(this, R.string.sharing_qq_weibo, Toast.LENGTH_LONG).show();
		/*Message msg = new Message();
		msg.what = MESSAGE;
		msg.arg1 = R.string.sharing_qq_weibo;
		mHandler.sendMessage(msg);
		int state = QQWeiboUtil.sendQQWeibo(this, mContent);
		if(state == 0){//发送成功
			Message msg1 = new Message();
			msg1.what = MESSAGE;
			msg1.arg1 = R.string.share_success;
			mHandler.sendMessage(msg1);
			finish();
		}else if(state == 1){//发送失败
			
		}else if(state ==2){//获取验证码失败
			
		}else if(state == 3){//access失败
			
		}*/
		
	}
	
	private void sendSinaWeibo(){
		SinaWeiboUtil.sendSinaWeibo(this, "");
	}
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case MESSAGE:
				int strResource = msg.arg1;
				Toast.makeText(DetailShareActivity.this, strResource, Toast.LENGTH_LONG).show();
				break;
			case SENDSINAWEIBO:
				sendSinaWeibo();
				break;
			}
		};
	};
	
	public void reSendSianWeibo(){
		mHandler.sendEmptyMessage(SENDSINAWEIBO);
	}
	
	
}
